

import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;
import java.util.Date;


import static java.lang.Float.parseFloat;


public class Company {
    static Scanner input = new Scanner(System.in);
    public Company(String num,String company,int amount)
    {
        this.num=num;
        this.company=company;
        this.amount=amount;}
    public Company(String num,String company)
    {
        this.num=num;
        this.company=company;
    }
    public String getnum(){return num;}
    public void setnum(String a){num=a;}
    public int getyear(){return year;}
    public int getmonth(){return month;}
    public int getdate(){return date;}
    public String getcompany(){return company;}
    public void setyear(int a){year=a;}
    public void setmonth(int a){month=a;}
    public void setdate(int a){date=a;}
    public void setcompany(String a){company=a;}
    public String getgoodsname(){return goodsname;}
    public void setgoodsname(String a){goodsname=a;}
    public int getamount(){return amount;}
    public void setamount(int a){amount=a;}
    public void inout()
    {

            System.out.println("1-入库 2-出库 3-一键入库 0-返回菜单");
            int a = input.nextInt();
            ArrayList<Company> list = new ArrayList<>();
            switch (a) {
                case 1:
                    System.out.print("请输入货物编号: ");
                    String goodsnum = input.next();
                    System.out.print("请输入货物名字: ");
                    String goodsname = input.next();
                    System.out.print("请输入货物数量: ");
                    int amount = input.nextInt();
                    System.out.print("时间:年 ");
                    int year = input.nextInt();
                    System.out.print("时间:月 ");
                    int month = input.nextInt();
                    System.out.print("时间:日 ");
                    int date = input.nextInt();
                    try{
                        BufferedReader in;
                        int t=0;
                        String line;
                        while (true) {
                            in = new BufferedReader(
                                    new InputStreamReader(
                                            new FileInputStream("Goods.txt")));
                            while ((line = in.readLine()) != null) {
                                String[] a1 = line.split(" ");
                                int s = amount;
                                if (a1[0].equals(goodsnum)) {
                                    t = 1;
                                    s = Integer.parseInt(a1[2])+amount;
                                    Company a2 = new Company(a1[0], a1[1], s);
                                    list.add(a2);
                                }else {
                                    Company a2 = new Company(a1[0], a1[1], Integer.parseInt(a1[2]));
                                    list.add(a2);
                                }


                            }PrintWriter out = new PrintWriter(
                                    new BufferedWriter(
                                            new OutputStreamWriter(
                                                    new FileOutputStream("Goods.txt"))));
                            for (int i = 0; i < list.size(); i++) {
                                out.println(list.get(i).num+" "+list.get(i).company + " "+list.get(i).amount);
                            }out.close();
                            if(t==0){PrintWriter out1 = new PrintWriter(
                                new BufferedWriter(
                                        new OutputStreamWriter(
                                                new FileOutputStream("Goods.txt",true))));
                        out1.println(goodsnum+" "+goodsname+" "+amount);
                                out1.close();}
                            break;
                        }
                        Admin.show();
                        System.out.println("入库成功!");
                        PrintWriter out1 = new PrintWriter(
                                new BufferedWriter(
                                        new OutputStreamWriter(
                                                new FileOutputStream("Sellcompany.txt",true))));
                        out1.println(num+" "+goodsname+" "+company+" "+year+" "+month+" "+date+" "+amount);
                        out1.close();
                    }catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    System.out.print("请输入货物编号: ");
                    goodsnum = input.next();
                    System.out.print("请输入货物名字: ");
                    goodsname = input.next();
                    System.out.print("请输入货物数量: ");
                    amount = input.nextInt();
                    System.out.print("时间:年 ");
                    year = input.nextInt();
                    System.out.print("时间:月 ");
                    month = input.nextInt();
                    System.out.print("时间:日 ");
                    date = input.nextInt();
                    try{
                        BufferedReader in;
                        int t=0;
                        String line;
                        while (true) {
                            in = new BufferedReader(
                                    new InputStreamReader(
                                            new FileInputStream("Goods.txt")));
                            while ((line = in.readLine()) != null) {
                                String[] a1 = line.split(" ");
                                int s = Integer.parseInt(a1[2]);
                                if (a1[0].equals(goodsnum)) {
                                    t = 1;
                                    s = Integer.parseInt(a1[2])-amount;
                                }if (s<0){System.out.println("货物数量不足！");return;}
                                if (s>0) {
                                    Company a2 = new Company(a1[0], a1[1], s);
                                    list.add(a2);
                                }
                            }PrintWriter out = new PrintWriter(
                                    new BufferedWriter(
                                            new OutputStreamWriter(
                                                    new FileOutputStream("Goods.txt"))));
                            for (int i = 0; i < list.size(); i++) {
                                out.println(list.get(i).num+" "+list.get(i).company + " "+list.get(i).amount);
                            }out.close();
                            break;
                        }
                        Admin.show();
                        System.out.println("出库成功!");
                        PrintWriter out1 = new PrintWriter(
                                new BufferedWriter(
                                        new OutputStreamWriter(
                                                new FileOutputStream("Buycompany.txt",true))));
                        out1.println(num+" "+goodsname+" "+company+" "+year+" "+month+" "+date+" "+amount);
                        out1.close();
                    }catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                case 3:
                    try{
                        BufferedReader in;
                        String line;String line1;
                        int s;
                        int e=0;

                        while (true) {
                            in = new BufferedReader(
                                    new InputStreamReader(
                                            new FileInputStream("Sellcompany.txt")));
                            while ((line = in.readLine()) != null) {
                                e=0;
                                String[] a1 = line.split(" ");
                                s = Integer.parseInt(a1[6]);
                                for (int i = 0; i < list.size(); i++) {
                                    if (Objects.equals(list.get(i).company, a1[1])){
                                        e =1;
                                        String x = list.get(i).num,y = a1[1];
                                        Company z = new Company(x,y,s+list.get(i).amount);
                                        list.set(i,z);
                                    }
                                }
                                Company a2 = new Company(a1[0], a1[1], s);
                                if (e==0)list.add(a2);
                            }
                            PrintWriter out = new PrintWriter(
                                    new BufferedWriter(
                                            new OutputStreamWriter(
                                                    new FileOutputStream("Goods.txt"))));
                            for (int i = 0; i < list.size(); i++) {
                                out.println(list.get(i).num+" "+list.get(i).company + " "+list.get(i).amount);
                            }out.close();
                            break;
                        }
                        Admin.show();
                        System.out.println("入库成功!");
                    }catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
            }

    }
    public static void search()
    {
        System.out.println("输入查询货物名字：");
        String goodsname = input.next();
        try{
            BufferedReader in;
            String line;
            while (true) {
                in = new BufferedReader(
                        new InputStreamReader(
                                new FileInputStream("Goods.txt")));
                while ((line = in.readLine()) != null) {
                    String[] a1 = line.split(" ");
                    if (a1[1].equals(goodsname)) {
                        System.out.println(a1[0]+" "+a1[1] + " "+a1[2]);
                        break;
                    }
                }
                break;
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    String num;
    private int year,month,date;
    String company;
    int amount;
    private String goodsname;
}
